/**
 * Copyright (c) 2018 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.SysMenuDTO;
import com.example.model.api.Result;
import com.example.security.InfoDetail;
import com.example.security.SecurityUser;
import com.example.service.SysMenuService;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.DefaultGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜单管理
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@RestController
@RequestMapping("menu")
@Tag(name = "菜单管理")
@Slf4j
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("nav")
    @Operation(summary = "导航")
    public Result<List<SysMenuDTO>> nav() {
        List<SysMenuDTO> list = sysMenuService.getUserMenuNavList();


        return new Result<List<SysMenuDTO>>().ok(list);
    }

    @GetMapping("permissions")
    @Operation(summary = "权限标识")
    public Result<Set<String>> permissions() {
        InfoDetail user = SecurityUser.getUser();
        Set<String> set = sysMenuService.getUserPermissions(user);

        return new Result<Set<String>>().ok(set);
    }


    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<List<SysMenuDTO>> list(@RequestParam(name = "type" ,required = false) Integer type) {
        List<SysMenuDTO> list = sysMenuService.getMenuList(type);
        return new Result<List<SysMenuDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
//    @PreAuthorize("hasAuthority('sys:menu:info')")
    public Result<SysMenuDTO> get(@PathVariable("id") Long id) {
        SysMenuDTO data = sysMenuService.get(id);

        return new Result<SysMenuDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
//    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result<String> save(@RequestBody SysMenuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysMenuService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
//    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result<String> update(@RequestBody SysMenuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysMenuService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
//    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result<String> delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id, "id");

        //判断是否有子菜单或按钮
        List<SysMenuDTO> list = sysMenuService.getListPid(id);
        if (list.size() > 0) {
            return new Result().error("ErrorCode.SUB_MENU_EXIST");
        }

        sysMenuService.delete(id);

        return new Result<String>().ok("ok");
    }

    @GetMapping("select")
    @Operation(summary = "当前登录用户的所有菜单")
//    @PreAuthorize("hasAuthority('sys:menu:select')")
    public Result<List<SysMenuDTO>> select() {
        List<SysMenuDTO> list = sysMenuService.getMenuList(null);

        return new Result<List<SysMenuDTO>>().ok(list);
    }

}